package com.demo.mykotlin.activity

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.demo.mykotlin.R
import kotlinx.android.synthetic.main.activity_page_list.*

/**
 * 页面列表（页面）
 */
class PageListActivity : BaseActivity() {
    private val activityBeanList = ArrayList<ActivityBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_page_list)
        initList()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        val adapter = Adapter(activityBeanList)
        recyclerView.adapter = adapter
    }

    /**
     * 初始化列表数据
     */
    private fun initList() {
        activityBeanList.add(ActivityBean(MainActivity::class.java, "主页面"))
        activityBeanList.add(ActivityBean(SecondActivity::class.java, "第二页面"))
        activityBeanList.add(ActivityBean(ListActivity::class.java, "list列表1页面"))
        activityBeanList.add(ActivityBean(List2Activity::class.java, "list列表2页面"))
        activityBeanList.add(ActivityBean(ChatActivity::class.java, "聊天列表页面"))
        activityBeanList.add(ActivityBean(RecycleActivity::class.java, "回收列表页面"))
        activityBeanList.add(ActivityBean(LoginActivity::class.java, "登录页面"))
        activityBeanList.add(ActivityBean(AllFinishActivity::class.java, "模拟强制下线页面"))
        activityBeanList.add(ActivityBean(ShowFragmentActivity::class.java, "显示碎片页面"))
        activityBeanList.add(ActivityBean(AnotherActivity::class.java, "动态替换碎片页面"))
        activityBeanList.add(ActivityBean(TimeChangeActivity::class.java, "广播监听时间变化"))
    }

    /**
     * 列表数据类，data修饰，无方法体时可以省略大括号
     */
    private data class ActivityBean(val cls: Class<*>, val name: String)

    /**
     * 列表适配器
     */
    private class Adapter(val list: List<ActivityBean>) :
        RecyclerView.Adapter<Adapter.ViewHolder>() {
        /**
         * 视图持有者
         */
        inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val jumpBtn: Button = view.findViewById(R.id.jumpBtn)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val layoutInflater = LayoutInflater.from(parent.context)
            val view = layoutInflater.inflate(R.layout.item_page_list, parent, false)

            return ViewHolder(view)
        }

        override fun getItemCount() = list.size

        override fun onBindViewHolder(holder: Adapter.ViewHolder, position: Int) {
            val activityBean = list[position]
            holder.jumpBtn.text = activityBean.name
            //设置条目监听
            holder.jumpBtn.setOnClickListener {
                val intent = Intent(holder.itemView.context, activityBean.cls)
                holder.itemView.context.startActivity(intent)
            }
        }
    }
}
